package com.flowershop.dao;

import com.flowershop.model.Flower;
import com.flowershop.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FlowerDao {
    
    // 添加花卉
    public boolean addFlower(Flower flower) {
        String sql = "INSERT INTO flowers (name, price, stock, category_id, description, image_url) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, flower.getName());
            pstmt.setBigDecimal(2, flower.getPrice());
            pstmt.setInt(3, flower.getStock());
            pstmt.setInt(4, flower.getCategoryId());
            pstmt.setString(5, flower.getDescription());
            pstmt.setString(6, flower.getImageUrl());
            
            return pstmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    // 更新花卉信息
    public boolean updateFlower(Flower flower) {
        String sql = "UPDATE flowers SET name=?, price=?, stock=?, category_id=?, description=?, image_url=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, flower.getName());
            pstmt.setBigDecimal(2, flower.getPrice());
            pstmt.setInt(3, flower.getStock());
            pstmt.setInt(4, flower.getCategoryId());
            pstmt.setString(5, flower.getDescription());
            pstmt.setString(6, flower.getImageUrl());
            pstmt.setInt(7, flower.getId());
            
            return pstmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    // 删除花卉
    public boolean deleteFlower(int id) {
        String sql = "DELETE FROM flowers WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    // 查询所有花卉
    public List<Flower> getAllFlowers() {
        List<Flower> flowers = new ArrayList<>();
        String sql = "SELECT * FROM flowers";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                Flower flower = new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setPrice(rs.getBigDecimal("price"));
                flower.setStock(rs.getInt("stock"));
                flower.setCategoryId(rs.getInt("category_id"));
                flower.setDescription(rs.getString("description"));
                flower.setImageUrl(rs.getString("image_url"));
                flower.setCreateTime(rs.getTimestamp("create_time"));
                flower.setUpdateTime(rs.getTimestamp("update_time"));
                flowers.add(flower);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flowers;
    }
    
    // 根据ID查询花卉
    public Flower getFlowerById(int id) {
        String sql = "SELECT * FROM flowers WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                Flower flower = new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setPrice(rs.getBigDecimal("price"));
                flower.setStock(rs.getInt("stock"));
                flower.setCategoryId(rs.getInt("category_id"));
                flower.setDescription(rs.getString("description"));
                flower.setImageUrl(rs.getString("image_url"));
                flower.setCreateTime(rs.getTimestamp("create_time"));
                flower.setUpdateTime(rs.getTimestamp("update_time"));
                return flower;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    // 根据分类ID查询花卉
    public List<Flower> getFlowersByCategory(int categoryId) {
        List<Flower> flowers = new ArrayList<>();
        String sql = "SELECT * FROM flowers WHERE category_id = ?";
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, categoryId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                Flower flower = new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setPrice(rs.getBigDecimal("price"));
                flower.setStock(rs.getInt("stock"));
                flower.setCategoryId(rs.getInt("category_id"));
                flower.setDescription(rs.getString("description"));
                flower.setImageUrl(rs.getString("image_url"));
                flower.setCreateTime(rs.getTimestamp("create_time"));
                flower.setUpdateTime(rs.getTimestamp("update_time"));
                flowers.add(flower);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return flowers;
    }
} 